<?php
/**
 * Created by PhpStorm.
 * User: cyc
 * Date: 2017/10/31
 * Time: 下午3:45
 */

class MsggBundler extends BundlerBase
{


    private $pid;
    private $type;
    private $status;
    private $min;
    private $titleGenerator;
    private $paramsGenerator;

    public function __construct($type, $status, $min, $pid = null, $bundleTimeStart = null, $bundleTimeEnd = null, $notice = true,$updateBundleItem =true)
    {
        $this->pid = $pid;
        $this->min = $min;
        $this->type = $type;
        $this->status = $status;
        $this->titleGenerator = function ($pid, $ids,$bundler){
            return 'msgg 提醒您，为了信息效果系统自动帮您合并了部分信息';
        };
        $this->paramsGenerator = function ($pid, $ids,$bundler) {
            $params = array();
            if ($bundler instanceof MsggBundler) {
                $params[]=['label'=>'合并开始时间','value'=>date('Y-h-d H-m-s',$bundler->getBundleTimeStart())];
                $params[]=['label'=>'合并结束时间','value'=>date('Y-h-d H-m-s',$bundler->getBundleTimeEnd())];
                $params[]=['label'=>'合并信息类型','value'=>is_array($bundler->getType()) ? implode(',', $bundler->getType()) :$bundler->getType()];
            }
            $params[] = ['label' => '合并信息数量', 'value' => sizeof($ids)];
            return $params;
        };

        parent::__construct($bundleTimeStart, $bundleTimeEnd, $notice,$updateBundleItem);
    }

    public function getBindableDatas()
    {
        $pid = $this->pid;
        $bindableDatas = array();
        if (empty($pid)) {
            $pid = array();
        }
        if (!is_array($pid)) {
            $pid = [$pid];
        }
        $status = $this->status;
        if (empty($status)) {
            $status = array();
        }
        if (!is_array($status)) {
            $status = array($status,);
        }
        $type = $this->type;
        if (empty($type)) {
            $status = array();
        }
        if (!is_array($type)) {
            $type = array($type,);
        }
        $types = $type;
        $pids = $pid;
        if (empty($pids)) {
            $criteria = new CDbCriteria();
            $criteria->select = 'pid';
            $criteria->addInCondition('status', $status);
            $criteria->addInCondition('type', $types);
            $criteria->addCondition('bid = 0');
            $criteria->addBetweenCondition('time', $this->bundleTimeStart, $this->bundleTimeEnd);
            $criteria->group = 'pid';
            $pids = array_map(function ($item) {
                return $item->pid;
            }, MsggItem::model()->findAll($criteria));

        }
        foreach ($pids as $pid) {
            $criteria = new CDbCriteria();
            $criteria->select = 'id';
            $criteria->addColumnCondition(['pid' => $pid]);
            $criteria->addInCondition('status', $status);
            $criteria->addInCondition('type', $types);
            $criteria->addCondition('bid = 0');
            $criteria->addBetweenCondition('time', $this->bundleTimeStart, $this->bundleTimeEnd);
            $ids = array_map(function ($item) {
                return $item->id;
            }, MsggItem::model()->findAll($criteria));
            if (empty($ids)||sizeof($ids) < $this->min) {
                Yii::log('ids size less min :' . $pid, CLogger::LEVEL_INFO, __METHOD__);
                continue;
            }
            if(empty($pid)||empty($person=AlarmPerson::model()->findByPk($pid))||!$person->usable)
            {
                $pid = self::util_admin_person()->id;
            }
            $title = $this->genTitle($pid, $ids);
            $params = $this->genParams($pid, $ids);
            $bindableDatas[] = $this->genData($pid, $ids, $params, $title);
        }

        return $bindableDatas;
    }

    public function setGenerator($titleGenFunc= null, $paramsGenFunc= null)
    {
        if (!empty($titleGenFunc)) {
            $this->titleGenerator = $titleGenFunc;
        }
        if (!empty($paramsGenFunc)) {
            $this->paramsGenerator = $paramsGenFunc;
        }
    }

    public function genTitle($pid, $ids)
    {
        $gen = $this->titleGenerator;
        return $gen($pid, $ids,$this);
    }

    public function genParams($pid, $ids)
    {
        $gen = $this->paramsGenerator;
        return $gen($pid, $ids,$this);
    }

    private function genData($pid, $ids, $params, $title)
    {
        return ['pid' => $pid, 'ids' => $ids, 'params' => $params, 'title' => $title,];
    }

    /**
     * @return null
     */
    public function getPid()
    {
        return $this->pid;
    }

    /**
     * @return null
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @return null
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * @return bool
     */
    public function isMin()
    {
        return $this->min;
    }

    /**
     * @return Closure
     */
    public function getTitleGenerator()
    {
        return $this->titleGenerator;
    }

    /**
     * @return Closure
     */
    public function getParamsGenerator()
    {
        return $this->paramsGenerator;
    }

    /**
     * @return bool
     */
    public function isNotice()
    {
        return $this->_notice;
    }

    /**
     * @return int|null
     */
    public function getBundleTimeStart()
    {
        return $this->bundleTimeStart;
    }

    /**
     * @return int|null
     */
    public function getBundleTimeEnd()
    {
        return $this->bundleTimeEnd;
    }



}